package com.fund.datacopy.web.controller.action;

import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fund.datacopy.bean.DocParser;
import com.fund.datacopy.task.thread.TaskPool;
import com.fund.datacopy.web.dto.ResponseDto;
import com.fund.datacopy.web.vo.RequestCopyVo;
import com.fund.datacopy.web.vo.RequestExtCopyVo;

@Controller
@RequestMapping({ "/cjob" })
public class CopyAction {
	
	@RequestMapping(value = { "/copy" })
	@ResponseBody
	public ResponseDto copyAction(String name) {
		try {
			if (StringUtils.isNotBlank(name)) {
				System.out.println(name);

				TaskPool pool = new TaskPool();
				pool.copy(name);
			} else {
				ResponseDto.failure("参数错误");
				System.out.println("request body error");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseDto.success("成功执行");
	}
	
	@RequestMapping(value = { "/cp" }, method = { RequestMethod.POST })
	@ResponseBody
	public ResponseDto copyAction(@RequestBody RequestCopyVo vo) {
		try {
			if (vo != null) {
				System.out.println(vo.getName());

				TaskPool pool = new TaskPool();
				DocParser doc = new DocParser();
				if (StringUtils.isNotBlank(vo.getName())) {
					doc.setName(vo.getName());
				}
				if (StringUtils.isNotBlank(vo.getSrc())) {
					doc.setSrc(vo.getSrc());
				}
				if (StringUtils.isNotBlank(vo.getDest())) {
					doc.setDest(vo.getDest());
				}
				if (StringUtils.isNotBlank(vo.getDb())) {
					doc.setSdb(vo.getDb());
					doc.setDdb(vo.getDb());
				}
				if (StringUtils.isNotBlank(vo.getTable())) {
					ArrayList<String> table = new ArrayList<String>();
					table.add(vo.getTable());
					doc.setStable(table);
					doc.setDtable(table);
				}

				if (StringUtils.isNotBlank(vo.getCollum())) {
					ArrayList<String> collum = new ArrayList<String>();
					collum.add(vo.getCollum());
					doc.setScollum(collum);
					doc.setDcollum(collum);
				}

				if (StringUtils.isNotBlank(vo.getStyle())) {
					ArrayList<String> style = new ArrayList<String>();
					style.add(vo.getStyle());
					doc.setStyle(style);
				}
				if (StringUtils.isNotBlank(vo.getExt())) {
					ArrayList<String> ext = new ArrayList<String>();
					ext.add(vo.getExt());
					doc.setExt(ext);
				}

				pool.execute(doc);
			} else {
				ResponseDto.failure("参数错误");
				System.out.println("request body error");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseDto.success("成功执行");
	}
	
	@RequestMapping(value = { "/cp/ext" }, method = { RequestMethod.POST })
	@ResponseBody
	public ResponseDto copyHisAction(@RequestBody RequestExtCopyVo vo) {
		try {
			if (vo != null) {
				System.out.println(vo.getName());

				TaskPool pool = new TaskPool();
				DocParser doc = new DocParser();
				if (StringUtils.isNotBlank(vo.getName())) {
					doc.setName(vo.getName());
				}
				if (StringUtils.isNotBlank(vo.getSrc())) {
					doc.setSrc(vo.getSrc());
				}
				if (StringUtils.isNotBlank(vo.getDest())) {
					doc.setDest(vo.getDest());
				}
				if (StringUtils.isNotBlank(vo.getSdb())) {
					doc.setSdb(vo.getSdb());
				}
				
				if (StringUtils.isNotBlank(vo.getDdb())) {
					doc.setDdb(vo.getDdb());
				}
				
				if (StringUtils.isNotBlank(vo.getStable())) {
					ArrayList<String> table = new ArrayList<String>();
					table.add(vo.getStable());
					doc.setStable(table);
				}
				
				if (StringUtils.isNotBlank(vo.getDtable())) {
					ArrayList<String> table = new ArrayList<String>();
					table.add(vo.getDtable());
					doc.setDtable(table);
				}


				if (StringUtils.isNotBlank(vo.getCollum())) {
					ArrayList<String> collum = new ArrayList<String>();
					collum.add(vo.getCollum());
					doc.setScollum(collum);
					doc.setDcollum(collum);
				}
				

				if (StringUtils.isNotBlank(vo.getExt())) {
					ArrayList<String> ext = new ArrayList<String>();
					ext.add(vo.getExt());
					doc.setExt(ext);
				}

				pool.execute(doc);
			} else {
				ResponseDto.failure("参数错误");
				System.out.println("request body error");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseDto.success("成功执行");
	}
	
}
